package cc.jq1024.domain.service.impl;

import cc.jq1024.domain.service.IDCCService;
import cc.jq1024.config.DCCAutoConfigProperties;
import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * @author li--jiaqiang
 * @date 2024−07−31
 */
@Service("dCCService")
public class DCCService implements IDCCService {

    private static final Logger log = LoggerFactory.getLogger(DCCService.class);
    private final CuratorFramework zookeeperClient;
    private final DCCAutoConfigProperties properties;

    public DCCService(CuratorFramework zookeeperClient, DCCAutoConfigProperties properties) {
        this.zookeeperClient = zookeeperClient;
        this.properties = properties;
    }

    /**
     * 修改dcc配置信息
     * @param key 字段名
     * @param value 字段值
     */
    @Override
    public void set(String key, String value) throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath().concat("/").concat(key);
        if (null == zookeeperClient.checkExists().forPath(keyPath)) {
            zookeeperClient.create().creatingParentsIfNeeded().forPath(keyPath);
        }
        zookeeperClient.setData().forPath(keyPath, value.getBytes(StandardCharsets.UTF_8));
        log.info("set dcc 【key:{} -》 value:{}】 success! path is {}", key, value, keyPath);
    }

    @Override
    public void setIfPresent(String key, String value) throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath().concat("/").concat(key);
        if (null == zookeeperClient.checkExists().forPath(keyPath)) {
            return;
        }
        zookeeperClient.setData().forPath(keyPath, value.getBytes(StandardCharsets.UTF_8));
        log.info("setIfPresent dcc 【key:{} -》 value:{}】 success! path is {}", key, value, keyPath);
    }

    /**
     * 删除节点
     * @param key 字段名
     */
    @Override
    public void deleteDataByKey(String key) throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath().concat("/").concat(key);
        zookeeperClient.delete().deletingChildrenIfNeeded().forPath(keyPath);
    }

    /**
     * 安全删除节点
     * @param key  字段名
     */
    @Override
    public void guaranteedDeleteData(String key) throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath().concat("/").concat(key);
        zookeeperClient.delete().guaranteed().forPath(keyPath);
    }

    /**
     * 获取子节点下的全部子节点路径集合
     */
    @Override
    public List<String> watchedGetChildren() throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath();
        List<String> list = zookeeperClient.getChildren().watched().forPath(keyPath);
        if (null == list || list.isEmpty()) return Collections.emptyList();
        return list;
    }

    /**
     * 异步修改数据
     */
    @Override
    public void setAsync(String key, String data) throws Exception {
        String keyPath = properties.getZookeeperBaseConfigPath().concat("/").concat(key);
        zookeeperClient.setData().inBackground().forPath(keyPath, data.getBytes(StandardCharsets.UTF_8));
    }
}